/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ECovoiturage. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.ui.page.root.trajetmidi;

import com.smouquet.ecovoiturage.core.bean.TrajetMidiItem;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Component;
import com.vaadin.ui.Table;
import com.vaadin.util.ReflectTools;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Time;
import java.text.DateFormat;
import java.util.List;

/**
 * Tableau correspondant à la liste des trajets du midi.
 * 
 * @author sebastien.mouquet
 */
public class TrajetMidiTable extends Table
{
    private DateFormat dfDefault;
    private List<TrajetMidiItem> trajets;
    
    
    public TrajetMidiTable()
    {
        this.dfDefault = DateFormat.getTimeInstance(DateFormat.SHORT);
        setImmediate(true);
        setSelectable(true);
        bindListener();
    }
    
    public TrajetMidiTable(List<TrajetMidiItem> trajets)
    {
        this();
        this.trajets = trajets;
        prepareAndInstallDataSource();
    }

    
    public void setTrajets(List<TrajetMidiItem> trajets)
    {
        this.trajets = trajets;
        prepareAndInstallDataSource();
    }
    
    private void bindListener()
    {
        final TrajetMidiTable table = this;
        
        addValueChangeListener(new ValueChangeListener()
        {
            @Override
            public void valueChange(Property.ValueChangeEvent event)
            {
                fireEvent(new SelectedItemChangedEvent(table, (TrajetMidiItem)event.getProperty().getValue()));
            }
        });
    }
    
    private void prepareAndInstallDataSource()
    {
        BeanItemContainer<TrajetMidiItem> container = new BeanItemContainer<TrajetMidiItem>(TrajetMidiItem.class);

        for(TrajetMidiItem t : trajets) {
            container.addItem(t);
        }
        
        setContainerDataSource(container);
        
        setColumnHeader("lieuArrivee", "Lieu");
        setColumnHeader("heureDepart", "Départ");
        setColumnHeader("heureRetour", "Retour");
        setColumnHeader("nbInteresses", "Interessés");
        setColumnHeader("nbPlaces", "Nb. places");
        
        setColumnWidth("heureDepart", 150);
        setColumnWidth("heureRetour", 150);
        setColumnWidth("nbInteresses", 200);
        setColumnWidth("nbPlaces", 200);
        
        setColumnAlignment("heureDepart", Align.CENTER);
        setColumnAlignment("heureRetour", Align.CENTER);
        setColumnAlignment("nbInteresses", Align.RIGHT);
        setColumnAlignment("nbPlaces", Align.RIGHT);
        
        setVisibleColumns(new Object[] {"lieuArrivee", "heureDepart", "heureRetour", "nbInteresses", "nbPlaces"});
    }

    
    @Override
    protected String formatPropertyValue(Object rowId, Object colId, Property<?> property)
    {
        if(Time.class.equals(property.getType()) && property.getValue() != null)
        {
            return dfDefault.format((Time)property.getValue());
        }
        
        return super.formatPropertyValue(rowId, colId, property);
    }

    
    public static class SelectedItemChangedEvent extends Event
    {
        private TrajetMidiItem item;
        
        public SelectedItemChangedEvent(Component source, TrajetMidiItem item) {
            super(source);
            this.item = item;
        }

        public TrajetMidiItem getItem() {
            return item;
        }
    }
    
    public interface SelectedItemChangedListener extends Serializable
    {
        public static final Method METHOD = ReflectTools.findMethod(SelectedItemChangedListener.class, "onSelectedItemChanged", SelectedItemChangedEvent.class);
        
        void onSelectedItemChanged(SelectedItemChangedEvent event);
    }
    
    public void addSelectedItemChangedListener(SelectedItemChangedListener listener)
    {
        addListener(SelectedItemChangedEvent.class, listener, SelectedItemChangedListener.METHOD);
    }
    
    public void removeSelectedItemChangedListener(SelectedItemChangedListener listener)
    {
        addListener(SelectedItemChangedEvent.class, listener, SelectedItemChangedListener.METHOD);
    }
}
